package com.jsmile.cloud.accountcenter.shop.repo;

import java.util.Objects;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jsmile.cloud.accountcenter.api.req.shop.ReqSupplyQuery;
import com.jsmile.cloud.accountcenter.shop.dao.SupplyDao;
import com.jsmile.cloud.accountcenter.shop.model.Supply;

import lombok.extern.slf4j.Slf4j;

/**
 * @author 龚亮
 * @version 1.0
 * @desc 为了薪资破万，还是睡一会儿吧...!
 * @date 2020年06月04日 04:39:24
 */
@Service
@Slf4j
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class SupplySearchRepository extends ServiceImpl<SupplyDao, Supply> {

    public IPage<Supply> pageQuery(ReqSupplyQuery reqSupplyQuery) {
        return this.baseMapper.selectPage(reqSupplyQuery.getPage(), getQueryWrapper(reqSupplyQuery));
    }

    public Wrapper getQueryWrapper(ReqSupplyQuery reqSupplyQuery) {
        return new QueryWrapper<Supply>().lambda().eq(null != reqSupplyQuery.getEnable(), Supply::getEnable, reqSupplyQuery.getEnable())
            .eq(null != reqSupplyQuery.getCurTenantId(), Supply::getTenantId, reqSupplyQuery.getCurTenantId())
            .eq(null != reqSupplyQuery.getSupplyId(), Supply::getId, reqSupplyQuery.getSupplyId())
            .like(Objects.nonNull(reqSupplyQuery.getSupplyNo()), Supply::getSupplyNo, reqSupplyQuery.getSupplyNo())
            .like(Objects.nonNull(reqSupplyQuery.getContactName()), Supply::getContactName, reqSupplyQuery.getContactName())
            .like(Objects.nonNull(reqSupplyQuery.getSupplyName()), Supply::getSupplyName, reqSupplyQuery.getSupplyName())
            .like(Objects.nonNull(reqSupplyQuery.getContactPhone()), Supply::getContactPhone, reqSupplyQuery.getContactPhone())
            .ge(null != reqSupplyQuery.getStartDate(), Supply::getCreateTime, reqSupplyQuery.getStartDate())
            .le(null != reqSupplyQuery.getEndDate(), Supply::getCreateTime, reqSupplyQuery.getEndDate()).orderByDesc(Supply::getCreateTime);// sort,createTime倒序
    }
}
